﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Threading;
using Microsoft.Reporting.WebForms;
public partial class Modules_IA_controlCenter : System.Web.UI.Page
{ 
    //读取数据库连接语句
    private string sqlConnStr = System.Configuration.ConfigurationManager.ConnectionStrings["smartGardenConnectionString"].ConnectionString;

    //实例化一个智能花园服务对象
    ServiceReference1.SampleServiceSoapClient smartGardent = new ServiceReference1.SampleServiceSoapClient();
    //Reference.SampleServiceSoapClient smartGardent = new smartGardent.SampleServiceSoapClient();

    protected void Page_Load(object sender, EventArgs e)
    {
        //载入所有模式名称
        LoadModeName();
    }

    #region 加载所有模式名称
    /// <summary>
    /// 加载所有模式名称
    /// </summary>
    private void LoadModeName()
    {

        //DropDownListModeName.Items.Clear();
        //统计GetArea()返回的数组对象的元素个数
        int count = smartGardent.GetMode().Count();

        //使用弱化类型接受服务返回的数组
        var areaArray = smartGardent.GetMode();

        //遍历服务返回数组元素，并在下拉框动态添加 
        for (int i = 0; i < count; i++)
        {
            if (DropDownListModeName.Items.FindByText(areaArray[i].ToString().Trim()) == null && areaArray[i].ToString().Trim() != "自定义")
            {
                DropDownListModeName.Items.Add(areaArray[i].ToString().Trim());
            }
        }

    }
    #endregion

    //区域号下拉框，选项改变值 事件
    protected void DropDownListArea_SelectedIndexChanged(object sender, EventArgs e)
    {
        dataQuery();
        ReportViewer1.LocalReport.Refresh();
    }

    #region 数据显示窗口显示数据
    /// <summary>
    /// 数据显示窗口显示数据
    /// </summary>
    private void dataQuery()
    {
        string modeId = smartGardent.GetModeId(DropDownListModeName.Text);
        int area = int.Parse(DropDownListArea.Text);
        var data = smartGardent.GetData(modeId, area);
        smartGardent.GetBaoBiao(DropDownListArea.Text, modeId);

        LabelTime.Text = GetNoralTime(data[5].ToString()).ToString();
        LabelMode.Text = DropDownListModeName.Text;
        LabelQueryArea.Text = area.ToString();
        LabelNowTemp.Text = data[0].ToString() + " ℃";
        LabelNowHum.Text = data[1].ToString() + " %";
        LabelNowIll.Text = data[2].ToString() + " KUL";
        LabelNowSoilHum.Text = data[4].ToString() + " %";

        LabelNowTemp0.Text = data[0].ToString() + " ℃";
        LabelNowHum0.Text = data[1].ToString() + " %";
        LabelNowIll0.Text = data[2].ToString() + " KUL";
        LabelNowSoilHum0.Text = data[4].ToString() + " %";
        if (data[3].ToString() == "0")
        {
            LabelNowRain.Text = "晴天";
        }
        else
        {
            LabelNowRain.Text = "雨天";
        }

        
    }
    #endregion

    #region 时间戳转为C#格式时间
    /// <summary>
    /// 时间戳转为C#格式时间
    /// </summary>
    /// <param name=”timeStamp”></param>
    /// <returns></returns>
    private DateTime GetNoralTime(string now)
    {
        string timeStamp = now;
        DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
        long lTime = long.Parse(timeStamp + "0000000");
        TimeSpan toNow = new TimeSpan(lTime);
        DateTime dtResult = dtStart.Add(toNow);
        return dtResult;
    }
    #endregion

    #region 开启或关闭自动模式
    protected void butAutoModel_Click(object sender, EventArgs e)
    {
        string modeId = smartGardent.GetModeId(DropDownListModeName.Text);
        int area = int.Parse(DropDownListArea.Text);
        //开启自动模式
        if (butAutoModel.Text == "开启自动模式")
        {
            string tempMin = textBoxTempMin.Text;
            string tempMax = textBoxTempMax.Text;
            string illMin = textBoxIllMin.Text;
            string illMax = textboxIllMax.Text;
            string soilHumMin = textBoxSoilHumMin.Text;
            string soilHumMax = textBoxSoilHumMax.Text;

            //将输入的参数插入数据库
            smartGardent.getAutoValue("WEB", modeId + "-" + area.ToString() + "-" + tempMin + "-" + tempMax + "-" + illMin + "-" + illMax + "-" + soilHumMin + "-" + soilHumMax);
            butAutoModel.Text = "关闭自动模式";
            LabelAreaState.Text = "自动模式";

            //各个按钮显示/隐藏
            butVisableOrEnable("开启自动模式");

        }
        //关闭自动模式
        else
        {
            smartGardent.switchManu("WEB", int.Parse(modeId), area);

            LabelAreaState.Text = "-";
            butAutoModel.Text = "开启自动模式";

            //各个按钮显示/隐藏
            butVisableOrEnable("关闭自动模式");
        }
    }
    #endregion

    #region 各个按钮点击后，其他按钮显示/隐藏
    /// <summary>
    /// 各个按钮点击后，其他按钮显示/隐藏
    /// </summary>
    /// <param name="actionName">按钮名称或操作名称</param>
    private void butVisableOrEnable(string actionName)
    {
        #region 开启自动模式
        if (actionName == "开启自动模式")
        {
            butManModel.Enabled = false;

            textBoxTempMin.Enabled = false;
            textBoxTempMax.Enabled = false;
            textBoxIllMin.Enabled = false;
            textboxIllMax.Enabled = false;
            textBoxSoilHumMin.Enabled = false;
            textBoxSoilHumMax.Enabled = false;

            butFanOpen.Enabled = false;
            butFanClose.Enabled = false;
            butPumpOpen.Enabled = false;
            butPumpClose.Enabled = false;
            butHotOpen.Enabled = false;
            butHotClose.Enabled = false;
            butLightOpen.Enabled = false;
            butLightClose.Enabled = false;
            butWinClose.Enabled = false;
            butWinOpen.Enabled = false;
        }
        #endregion

        #region 关闭自动模式
        if (actionName == "关闭自动模式")
        {
            butManModel.Enabled = true;

            textBoxTempMin.Enabled = true;
            textBoxTempMax.Enabled = true;
            textBoxIllMin.Enabled = true;
            textboxIllMax.Enabled = true;
            textBoxSoilHumMin.Enabled = true;
            textBoxSoilHumMax.Enabled = true;

            butFanOpen.Enabled = true;
            butFanClose.Enabled = true;
            butPumpOpen.Enabled = true;
            butPumpClose.Enabled = true;
            butHotOpen.Enabled = true;
            butHotClose.Enabled = true;
            butLightOpen.Enabled = true;
            butLightClose.Enabled = true;
            butWinClose.Enabled = true;
            butWinOpen.Enabled = true;
        }
        #endregion

        #region 开启手动模式
        if (actionName == "开启手动模式")
        {
            textBoxTempMin.Enabled = false;
            textBoxTempMax.Enabled = false;
            textBoxIllMin.Enabled = false;
            textboxIllMax.Enabled = false;
            textBoxSoilHumMin.Enabled = false;
            textBoxSoilHumMax.Enabled = false;

            butAutoModel.Enabled = false;

            //各个开关隐藏
            butFanOpen.Enabled = true;
            butFanClose.Enabled = true;
            butPumpClose.Enabled = true;
            butPumpOpen.Enabled = true;
            butHotClose.Enabled = true;
            butHotOpen.Enabled = true;
            butLightClose.Enabled = true;
            butLightOpen.Enabled = true;
            butWinClose.Enabled = true;
            butWinOpen.Enabled = true;
        }
        #endregion

        #region 关闭手动模式
        if (actionName == "关闭手动模式")
        {
            textBoxTempMin.Enabled = true;
            textBoxTempMax.Enabled = true;
            textBoxIllMin.Enabled = true;
            textboxIllMax.Enabled = true;
            textBoxSoilHumMin.Enabled = true;
            textBoxSoilHumMax.Enabled = true;

            butAutoModel.Enabled = true;

            //各个开关隐藏
            butFanOpen.Enabled = false;
            butFanClose.Enabled = false;
            butPumpClose.Enabled = false;
            butPumpOpen.Enabled = false;
            butHotClose.Enabled = false;
            butHotOpen.Enabled = false;
            butLightClose.Enabled = false;
            butLightOpen.Enabled = false;
            butWinClose.Enabled = false;
            butWinOpen.Enabled = false;
        }
        #endregion
    }
    #endregion

    #region 各个设备开/关 按钮事件
    /// <summary>
    /// 各个设备开/关 按钮事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void butFanOpen_Click(object sender, EventArgs e)
    {
        FacSwitch("1", "M1");
        lableFanState.Text = "开";
    }

    protected void butFanClose_Click(object sender, EventArgs e)
    {
        FacSwitch("0", "M1");
        lableFanState.Text = "关";
    }

    protected void butWinOpen_Click(object sender, EventArgs e)
    {
        FacSwitch("1", "M4");
        lableWinState.Text = "开";
    }

    protected void butWinClose_Click(object sender, EventArgs e)
    {
        FacSwitch("0", "M4");
        lableWinState.Text = "关";
    }

    protected void butHotOpen_Click(object sender, EventArgs e)
    {
        FacSwitch("1", "M2");
        lableHotLigState.Text = "开";
    }

    protected void butHotClose_Click(object sender, EventArgs e)
    {
        FacSwitch("0", "M2");
        lableHotLigState.Text = "关";
    }

    protected void butLightOpen_Click(object sender, EventArgs e)
    {
        FacSwitch("1", "M5");
        lableLightState.Text = "开";
    }

    protected void butLightClose_Click(object sender, EventArgs e)
    {
        FacSwitch("0", "M5");
        lableLightState.Text = "关";
    }

    protected void butPumpOpen_Click(object sender, EventArgs e)
    {
        FacSwitch("1", "M3");
        lablePumpState.Text = "开";
    }

    protected void butPumpClose_Click(object sender, EventArgs e)
    {
        FacSwitch("0", "M3");
        lablePumpState.Text = "关";
    }

    private void FacSwitch(string state, string facNO)
    {
        string modeId = smartGardent.GetModeId(DropDownListModeName.Text);
        int area = int.Parse(DropDownListArea.Text);
        smartGardent.ManFacAction("WEB", state, modeId, area.ToString(), facNO);
    }

    #endregion

    #region 手动模式开关按钮
    protected void butManModel_Click(object sender, EventArgs e)
    {
        //开启手动模式
        if (butManModel.Text == "开启手动模式")
        {
            LabelAreaState.Text = "手动模式";
            string modeId = smartGardent.GetModeId(DropDownListModeName.Text);
            int area = int.Parse(DropDownListArea.Text);
            //将操作记录插入数据库
            smartGardent.switchManu("WEB", int.Parse(modeId), area);
            butManModel.Text = "关闭手动模式";

            //开启手动模式各个按钮 显示/隐藏
            butVisableOrEnable("开启手动模式");
        }

        //关闭手动模式
        else if (butManModel.Text == "关闭手动模式")
        {
            //关闭手动模式各个按钮 显示/隐藏
            butVisableOrEnable("关闭手动模式");

            butManModel.Text = "开启手动模式";

            LabelAreaState.Text = "-";
        }

    }
    #endregion

    protected void DropDownListModeName_SelectedIndexChanged(object sender, EventArgs e)
    {
        //string modeName = DropDownListModeName.Text;
        //int count = smartGardent.GetModeAllArea(modeName).Count();
        ////统计GetArea()返回的数组对象的元素个数

        ////使用弱化类型接受服务返回的数组
        //var areaArray = smartGardent.GetModeAllArea(modeName);

        ////遍历服务返回数组元素，并在下拉框动态添加 
        //for (int i = 0; i < count; i++)
        //{
        //    DropDownListArea.Items.Add(areaArray[i].ToString().Trim());
        //    lableFanState.Text += areaArray[i].ToString().Trim();
        //}
    }

    protected void Timer1_Tick(object sender, EventArgs e)
    {
        dataQuery();
        ReportViewer1.LocalReport.Refresh();
    }


    protected void ReportViewer1_DataBinding(object sender, EventArgs e)
    {
        string modeId = smartGardent.GetModeId(DropDownListModeName.Text);
        int area = Convert.ToInt32(DropDownListArea.Text);
        var tab = smartGardent.GetData(modeId,area);

    }
}